﻿using Blog.Application.Contract.User;
using Blog.Application.Contract.User.Dto;
using Blog.HttpApi.Host.Options;
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace Blog.HttpApi.Host.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserServer _userServer;
        private readonly JwtOptions _jwtOptions;

        public UserController(IUserServer userServer, IOptions<JwtOptions> jwtOptions)
        {
            _userServer = userServer;
            _jwtOptions = jwtOptions.Value;
        }

        /// <summary>
        /// 注册账号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task CreateUserAsync(CreateUserDto input)
        {
            await _userServer.CreateUserAsync(input);
        }

        [HttpPost("login")]
        public async Task<string> LoginAsync(LoginDto input)
        {
            var user = await _userServer.LoginAsync(input);
            var claims = new[] {
                new Claim (Constant.Id,user.Id .ToString ()  ),
                new Claim (ClaimTypes.Role ,user.Role )
            };
            var cred = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.SecretKey)),
                SecurityAlgorithms.HmacSha256);

            var jwtSecurityToken = new JwtSecurityToken(
                _jwtOptions.Issuer, // 签发者
                _jwtOptions.Audience,   //接收者
                claims,    //payload
                expires: DateTime.Now.AddMinutes(_jwtOptions.ExpireMinutes),  // 过期时间
                signingCredentials: cred); // 令牌
            // 签发token
            var token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
            return token;
        }

    }
}
